#include "weavemotionconditiondialog.h"
#include "ui_weavemotionconditiondialog.h"
#include <QMessageBox>
#include <QDebug>

WeaveMotionConditionDialog::WeaveMotionConditionDialog(int styleId, CommunicateNormalDelegate *communicateNormalIn, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::WeaveMotionConditionDialog)
{
    ui->setupUi(this);
    move(0,0);
    initialDisplayStyle(styleId);
    communicateNormalDelegate = communicateNormalIn;
}

WeaveMotionConditionDialog::~WeaveMotionConditionDialog()
{
    delete ui;
}

void WeaveMotionConditionDialog::initialDisplayStyle(int styleId)
{
    switch (styleId) {
    case 1:
        this->setGeometry(0,0,600,450);

        ui->returnBtn->setGeometry(500,400,70,40);
        ui->saveBtn->setGeometry(500,350,70,40);
        break;
    default:
        break;
    }
}

void WeaveMotionConditionDialog::showView_slot()
{
    this->show();
    updateConditionSHow(ui->comboBox_index->currentIndex());
}

void WeaveMotionConditionDialog::updateConditionSHow(int index)
{
    std::vector<struct WeaveMotionCondition> configListOut;
    if(communicateNormalDelegate->getWeaveMotionConditionInfo(configListOut)==1)
    {
     qDebug() << "listCount:" << configListOut.size();
    }

    struct WeaveMotionCondition weldCondition;
    weldCondition = configListOut.at(index);
    updateUiShow(weldCondition);
    QMessageBox::information(this, "提示", "参数获取成功!", QMessageBox::Yes );
}

void WeaveMotionConditionDialog::updateUiShow(WeaveMotionCondition weavedCondition)
{
    ui->comboBox_index->setCurrentIndex(weavedCondition.id);
    ui->comboBox_style->setCurrentIndex(weavedCondition.motionStyle);
    ui->lineEdit_frequency->setText(QString::number(weavedCondition.frequency));
    ui->lineEdit_width->setText(QString::number(weavedCondition.width));
    ui->lineEdit_widthMove->setText(QString::number(weavedCondition.width_move));
    ui->lineEdit_height->setText(QString::number(weavedCondition.height));
    ui->checkBox_isHalt->setChecked(weavedCondition.isHaltAtEdge);
    ui->lineEdit_leftHaltTime->setText(QString::number(weavedCondition.leftHaltTime));
    ui->lineEdit_rightHaltTime->setText(QString::number(weavedCondition.rightHaltTime));
    ui->lineEdit_xAngle->setText(QString::number(weavedCondition.rotateAngle_x));
    ui->lineEdit_zAngle->setText(QString::number(weavedCondition.rotateAngle_z));



}

void WeaveMotionConditionDialog::on_saveBtn_clicked()
{

    struct WeaveMotionCondition weavedCondition;
    weavedCondition.id = ui->comboBox_index->currentIndex();
    weavedCondition.motionStyle=ui->comboBox_style->currentIndex();
    weavedCondition.frequency=ui->lineEdit_frequency->text().toDouble();
    weavedCondition.width=ui->lineEdit_width->text().toDouble();
    weavedCondition.width_move=ui->lineEdit_widthMove->text().toDouble();
    weavedCondition.height=ui->lineEdit_height->text().toDouble();
    weavedCondition.isHaltAtEdge=ui->checkBox_isHalt->isChecked();
    weavedCondition.leftHaltTime=ui->lineEdit_leftHaltTime->text().toDouble();
    weavedCondition.rightHaltTime=ui->lineEdit_rightHaltTime->text().toDouble();
    weavedCondition.rotateAngle_x=ui->lineEdit_xAngle->text().toDouble();
    weavedCondition.rotateAngle_z=ui->lineEdit_zAngle->text().toDouble();



    int returnInfo;
    //gSOAP通信服务端修改信息
    communicateNormalDelegate->setWeaveMotionConditionInfo(weavedCondition,returnInfo);

    QMessageBox::information(this, "提示", "设置成功，即刻生效!", QMessageBox::Yes );
}

void WeaveMotionConditionDialog::on_returnBtn_clicked()
{
    this->hide();
}

void WeaveMotionConditionDialog::on_comboBox_index_currentIndexChanged(int index)
{
    updateConditionSHow(index);
}
